<?php
session_start();
if (!isset($_SESSION['user'])) {
    header("Location: login.php");
    exit;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
        <title>Nufashion</title>
        <link rel="stylesheet" href="../css/admin-style.css" type="text/css" media="all" />
        <script src="../js/jquery-1.6.4.min.js" type="text/javascript"></script>
        <script src="../js/jquery.validate.min.js" type="text/ecmascript"></script>
    </head>
    <body>
        <script type="text/javascript">
            function collectValue() {
                var string = jQuery("#proID").val() + ",";
                $("#relatedList[] option").each(function()
                {
                    string += $(this).val() + ",";
                });
                string = string.substring(0, (string.length  - 1) );
                return string;
            }
		
            function searchRelatedProduct() {
                jQuery.ajax({
                    type:"POST",
                    url:"../action/searchRelatedProductAction.php",
                    data: "keyword=" + jQuery("#keyword").val() + "&ids=" + collectValue(),
                    success: function(html){
                        jQuery("#availableList").html(html);
                    }
                });
            }
		
            $(document).ready(function() {
                $('#add').click(function() {
                    return !$('#availableList option:selected').remove().appendTo('#relatedList');
                });
			 
                $('#remove').click(function() {
                    return !$('#relatedList option:selected').remove().appendTo('#availableList');
                });
			 
                $('#form').submit(function() {
                    $('#relatedList option').each(function(i) {
                        $(this).attr("selected", "selected");
                    });
                });
				
                $("#form").validate({
                    rules: {
                        pro_name: "required",
                        price: {number: true, required: true},
                        img1: "required"
                    },
						
                    messages: {
                        pro_name: "Vui lòng nhập tên sản phẩm",
                        price: "Giá sản phẩm không hợp lệ",
                        img1: "Vui lòng chọn ảnh"
                    }
                });
			 
            });

            function selectAllOptions(selStr){
                var selObj = document.getElementById(selStr);
                for (var i=0; i<selObj.options.length; i++) {
                    selObj.options[i].selected = true;
                }
            }
			
            function hideMessage() {
                var ok = document.getElementById("msgOK");
                addClass(ok, 'msg-hidden');
            }
			
            function addClass(el, name)
            {
                el.className += (el.className ? ' ' : '') +name;
            }
		
        </script>
        <!-- Header -->
<?php include("header.php"); ?>
        <!-- End Header -->

        <!-- Container -->
        <div id="container">
            <div class="shell">

                <!-- Message OK -->
                <?php
                $message = "";
                $addMessage = $_GET['add_message'];
                if (isset($addMessage)) {
                    if ($addMessage == 'ok') {
                        $message = 'Sản phẩm mới được tạo thành công!';
                    } else {
                        $message = 'Tạo sản phẩm thất bại!';
                    }
                }

                if ($message == '') {
                    $class = 'msg msg-ok msg-hidden';
                } else {
                    $class = 'msg msg-ok';
                }
                ?>
                <div class="<?php echo($class); ?>" id="msgOK">
                    <p><strong><?php echo($message); ?></strong></p>
                    <a href="#" onclick="hideMessage()" class="close">close</a>
                </div>
                <!-- End Message OK -->

                <!-- Message Error -->
                <div class="msg msg-error msg-hidden">
                    <p><strong>You must select a file to upload first!</strong></p>
                    <a href="#" class="close">close</a>
                </div>
                <!-- End Message Error -->
                <br />
                <!-- Main -->
                <?php
                include("../service/productService.php");
                include("../service/categoryService.php");
                include("../service/productDetailService.php");
                include("../service/imageService.php");
                ?>
                <div id="main">
                    <div class="cl">&nbsp;</div>

                    <!-- Content -->
                    <div id="content">
                        <!-- Box -->
                        <div class="box">
                            <!-- Box Head -->
                            <div class="box-head">
                                <h2>Thêm Sản Phẩm</h2>
                            </div>
                            <!-- End Box Head -->

                            <form action="../action/addNewProductAction.php" method="post" enctype="multipart/form-data" name="form" id="form" >

                                <!-- Form -->
                                <div class="form">
                                    <p>
                                        <label>Tên Sản Phẩm</label>
                                        <input type="text" class="field size1" name="pro_name" id="pro_name" />
                                        <input type="hidden" name="proID" id="proID" value="0"/>
                                    </p>
                                    <p>
                                        <label>Mã Sản Phẩm</label>
                                        <input type="text" class="field size1" name="pro_code" id="pro_code" />
                                    </p>
                                    <?php
                                    $detailList = getAllDetail();
                                    while ($row = ConnectionManager::getInstance()->fetchArray()) {
                                        ?>
                                        <p>
                                            <label><?php echo($row['PRD_NAME']); ?></label>
                                            <input type="text" class="field size1" name="<?php echo($row['PRD_ID']); ?>" />
                                        </p>
                                        <?php
                                    }
                                    ?>

                                    <p>
                                        <label>Giá</label>
                                        <input type="text" class="field size1" name="price" />
                                    </p>

                                    <p>
                                        <label>Tình Trạng</label>
                                        <select class="field size1"  id="status" name="status">
                                            <?php
                                            $statusArray = array(1 => 'Còn Hàng', 2 => 'Hàng Mới Về', 3 => 'Hết Hàng', 4 => 'Hàng Sắp Về', 5 => 'Sale off', 6 => 'Ẩn');
                                            for ($i = 1; $i <= 6; $i++) {

                                                echo("<option value='" . $i . "'>" . $statusArray[$i] . "</option>");
                                            }
                                            ?>
                                        </select>
                                    </p>
                                    <p>
                                        <label>Chủng Loại:</label>
                                        <select class="field size1" id="category" name="category">
                                    <?php
                                    $cateResult = getNonParentCategory();
                                    while ($cateRow = ConnectionManager::getInstance()->fetchArray()) {
   
                                            echo("<option value='" . $cateRow['CAT_ID'] . "'>" . $cateRow['CAT_NAME'] . "</option>");
                                        
                                    }
                                    ?>
                                        </select>
                                    </p>
                                    <p>
                                        <label>Phụ kiện</label>
                                        <table>
                                            <tr>
                                                <td>
                                                    <input type="text" name="keyword" id="keyword" class="size5" value="" />
                                                    <input type="button" value="Tìm Kiếm" class="button right" onclick="searchRelatedProduct()"/>
                                                </td>
                                                <td></td>
                                                <td></td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    <select name="availableList[]" id="availableList" size="10" multiple="multiple" class="categorylist">
                                                        <?php
                                                        $availableRelationResult = getAvailableRelatedProductByID($productID);
                                                        while ($relationRow = ConnectionManager::getInstance()->fetchArray()) {
                                                            echo("<option value='" . $relationRow['PRO_ID'] . "'>" . $relationRow['PRO_NAME'] . "</option>");
                                                        }
                                                        ?>
                                                    </select>
                                                    <br /><center>Sản phẩm hiện có</center>
                                                </td>
                                                <td><input type="button" value=">" class="button" id="add" /><br /><input type="button" value="<" class="button" id="remove" /></td>
                                                <td>
                                                    <select name="relatedList[]" id="relatedList" size="10" multiple="multiple" class="categorylist">

                                                    </select>
                                                    <br /><center>Sản phẩm là phụ kiện cho sản phẩm này</center>
                                                </td>
                                            </tr>
                                        </table>
                                    </p>
                                    <p>
                                        <label>Hình ảnh (kích thước tối ưu 453 x 567 pixels)</label>
                                        <table>
                                            <tr>
                                                <td>Ảnh 1 (ảnh chính):</td>
                                                <td>
                                                    <input type="file" name="img1" id="img1" />
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>Ảnh 2:</td>
                                                <td>
                                                    <input type="file" name="img2"/>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>Ảnh 3:</td>
                                                <td><input type="file" name="img3"/>
                                                </td>
                                            </tr>
                                        </table>
                                    </p>
                                    <p>
                                        <label>Miêu tả </label>
                                        <textarea class="field size1" rows="10" cols="30" name="description"></textarea>
                                    </p>

                                </div>
                                <!-- End Form -->

                                <!-- Form Buttons -->
                                <div class="buttons">
                                    <input type="submit" class="button" value="Thêm Sản Phẩm" />
                                    <input type="reset" class="button" value="Xóa Biểu nhập" />
                                    <input type="button" class="button" value="Trở về" onclick="window.location = 'product.php'" />
                                </div>
                                <!-- End Form Buttons -->
                            </form>
                        </div>
                        <!-- End Box -->

                    </div>
                    <!-- End Content -->



                    <div class="cl">&nbsp;</div>
                </div>
                <!-- Main -->
            </div>
        </div>
        <!-- End Container -->

        <!-- Footer -->
        <div id="footer">
            <?php include("footer.php"); ?>
        </div>
        <!-- End Footer -->

    </body>
</html>
